package com.yanqu.road.server.cmd.activity.xiuxian;

import com.yanqu.road.entity.activity.xiuxian.config.XiuXianConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XiuXianProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.xiuxian.CrossXiuXianMgr;
import com.yanqu.road.server.manager.activity.xiuxian.activity.CrossXiuXianActivity;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_CROSS_XIU_XIAN_MARK_PLACE, desc = "标记地点")
public class CrossXiuXianMarkPlaceCmd extends CrossCommand {

    private int activityId;

    private long userId;

    private int placeId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XiuXianProto.CrossMarkPlaceReqMsg reqMsg = XiuXianProto.CrossMarkPlaceReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = packet.getPlayerId();
        placeId = reqMsg.getPlaceId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossXiuXianActivity activity = CrossXiuXianMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_XIU_XIAN_ACTIVITY_NOT_FOUND;
            return;
        }
        XiuXianConfig config = activity.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_XIU_XIAN_CONFIG_NOT_FOUND;
            return;
        }
        if (!activity.isOpenTime()) {
            ret = GameErrorCode.E_XIU_XIAN_PVP_NOT_OPEN;
            return;
        }
        ret = activity.markPlaceByCmd(packet.getPlayerId(), placeId);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            XiuXianProto.XiuXianMarkPlaceRespMsg.Builder respMsg = XiuXianProto.XiuXianMarkPlaceRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_XIU_XIAN_MARK_PLACE, respMsg));
        }
    }
}
